package srm529;

import java.util.Arrays;
import java.util.Comparator;

public class KingSort {
    public String[] getSortedList(String[] kings) {
        final String[] ones = new String[]{"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"};
        final String[] tens = new String[]{"X", "XX", "XXX", "XL", "L"};
        Comparator<String> comp = new Comparator<String>() {
            public int compare(String o1, String o2) {
                String[] sp = o1.split("\\s+");
                String[] sp2 = o2.split("\\s+");
                if (!sp[0].equals(sp2[0]))
                    return sp[0].compareTo(sp2[0]);
                return cmp(sp[1], sp2[1]);
            }

            private int cmp(String s, String s1) {
                int val1 = 0, val2 = 0;
                for (int i = 0; i < ones.length; i++) {
                    if (s.equals(ones[i])) {
                        val1 = i + 1;
                    }
                    if (s1.equals(ones[i])) {
                        val2 = i + 1;
                    }
                }
                for (int i = 0; i < tens.length; i++) {
                    if (s.equals(tens[i])) {
                        val1 = getVal(tens[i]);
                    }
                    if (s1.equals(tens[i])) {
                        val2 = getVal(tens[i]);
                    }
                }


                for (int i = 0; i < tens.length; i++) {
                    for (int j = 0; j < ones.length - 1; j++) {
                        String num = tens[i] + ones[j];

                        if (s.equals(num)) {
                            val1 = getVal(tens[i]) + j+1;
                        }
                        if (s1.equals(num)) {
                            val2 = getVal(tens[i]) + j+1;
                        }
                    }
                }
                return val1-val2;
            }

            private int getVal(String ten) {
                if (ten.equals("X")) return 10;
                else if (ten.equals("XX")) return 20;
                else if (ten.equals("XXX")) return 30;
                else if (ten.equals("XL")) return 40;
                else if (ten.equals("L")) return 50;
                else return 0;
            }
        };
        Arrays.sort(kings, comp);
        return kings;
    }

    // BEGIN KAWIGIEDIT TESTING
    // Generated by KawigiEdit 2.1.4 (beta) modified by pivanof
    private static boolean KawigiEdit_RunTest(int testNum, String[] p0, boolean hasAnswer, String[] p1) {
        System.out.print("Test " + testNum + ": [" + "{");
        for (int i = 0; p0.length > i; ++i) {
            if (i > 0) {
                System.out.print(",");
            }
            System.out.print("\"" + p0[i] + "\"");
        }
        System.out.print("}");
        System.out.println("]");
        KingSort obj;
        String[] answer;
        obj = new KingSort();
        long startTime = System.currentTimeMillis();
        answer = obj.getSortedList(p0);
        long endTime = System.currentTimeMillis();
        boolean res;
        res = true;
        System.out.println("Time: " + (endTime - startTime) / 1000.0 + " seconds");
        if (hasAnswer) {
            System.out.println("Desired answer:");
            System.out.print("\t" + "{");
            for (int i = 0; p1.length > i; ++i) {
                if (i > 0) {
                    System.out.print(",");
                }
                System.out.print("\"" + p1[i] + "\"");
            }
            System.out.println("}");
        }
        System.out.println("Your answer:");
        System.out.print("\t" + "{");
        for (int i = 0; answer.length > i; ++i) {
            if (i > 0) {
                System.out.print(",");
            }
            System.out.print("\"" + answer[i] + "\"");
        }
        System.out.println("}");
        if (hasAnswer) {
            if (answer.length != p1.length) {
                res = false;
            } else {
                for (int i = 0; answer.length > i; ++i) {
                    if (!answer[i].equals(p1[i])) {
                        res = false;
                    }
                }
            }
        }
        if (!res) {
            System.out.println("DOESN'T MATCH!!!!");
        } else if ((endTime - startTime) / 1000.0 >= 2) {
            System.out.println("FAIL the timeout");
            res = false;
        } else if (hasAnswer) {
            System.out.println("Match :-)");
        } else {
            System.out.println("OK, but is it right?");
        }
        System.out.println("");
        return res;
    }

    public static void main(String[] args) {
        boolean all_right;
        all_right = true;

        String[] p0;
        String[] p1;

        // ----- test 0 -----
        p0 = new String[]{"Louis IX", "Louis VIII"};
        p1 = new String[]{"Louis VIII", "Louis IX"};
        all_right = KawigiEdit_RunTest(0, p0, true, p1) && all_right;
        // ------------------

        // ----- test 1 -----
        p0 = new String[]{"Louis IX", "Philippe II"};
        p1 = new String[]{"Louis IX", "Philippe II"};
        all_right = KawigiEdit_RunTest(1, p0, true, p1) && all_right;
        // ------------------

        // ----- test 2 -----
        p0 = new String[]{"Richard III", "Richard I", "Richard II"};
        p1 = new String[]{"Richard I", "Richard II", "Richard III"};
        all_right = KawigiEdit_RunTest(2, p0, true, p1) && all_right;
        // ------------------

        // ----- test 3 -----
        p0 = new String[]{"John X", "John I", "John L", "John V"};
        p1 = new String[]{"John I", "John V", "John X", "John L"};
        all_right = KawigiEdit_RunTest(3, p0, true, p1) && all_right;
        // ------------------

        // ----- test 4 -----
        p0 = new String[]{"Philippe VI", "Jean II", "Charles V", "Charles VI", "Charles VII", "Louis XI"};
        p1 = new String[]{"Charles V", "Charles VI", "Charles VII", "Jean II", "Louis XI", "Philippe VI"};
        all_right = KawigiEdit_RunTest(4, p0, true, p1) && all_right;
        // ------------------

        // ----- test 5 -----
        p0 = new String[]{"Philippe II", "Philip II"};
        p1 = new String[]{"Philip II", "Philippe II"};
        all_right = KawigiEdit_RunTest(5, p0, true, p1) && all_right;
        // ------------------

        if (all_right) {
            System.out.println("You're a stud (at least on the example cases)!");
        } else {
            System.out.println("Some of the test cases had errors.");
        }
    }
    // END KAWIGIEDIT TESTING
}
//Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
